/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package aicue1.singletons;

import aicue1.AICUE1Util.Payment_Preference;
import aicue1.beans.Customer;
import java.util.ArrayList;

/**
 *
 * @author Thomas
 */
public final class CustomersSingleton
{
    /**
     * Privates Klassenattribut,
     * wird beim erstmaligen Gebrauch (nicht beim Laden) der Klasse erzeugt
     */
    private static CustomersSingleton customersInstance = null;

    private ArrayList<Customer> customersList = null;

    /** Konstruktor ist privat, Klasse darf nicht von außen instanziiert werden. */
    private CustomersSingleton() 
    {
        customersList = new ArrayList<Customer>();
    }

    /**
     * Statische Methode "getInstance()" liefert die einzige Instanz der Klasse zurück.
     * Ist synchronisiert und somit thread-sicher.
     */
    public synchronized static CustomersSingleton getInstance()
    {
        if (customersInstance == null) {
           customersInstance = new CustomersSingleton();
        }
        return customersInstance;
    }

    public synchronized Boolean doesCustomerExist(Customer customer)
    {
        return customersList.contains(customer);
    }

    public synchronized Boolean insertCustomer(Customer customer)
    {
        Boolean insertOk = false;
        if(!customersList.contains(customer))
        {
            customersList.add(customer);
            insertOk = true;
        }
        return insertOk;
    }

    public synchronized Boolean updateCustomer(String id, Customer customer)
    {
        int index = customersList.indexOf(new Customer(id,null,null,null));
        if(index == -1)
            return false;
        else
        {
            customersList.set(index, customer);
            return true;
        }
        
    }

    public synchronized Customer getCustomer(String id)
    {
        int index = customersList.indexOf(new Customer(id,null,null,null));
        if(index == -1)
            return null;
        else
            return customersList.get(index);
    }

    public synchronized Boolean deleteCustomer(String id)
    {
        return customersList.remove(new Customer(id,null,null,null));
    }

    public synchronized void clearList()
    {
         customersList.clear();
    }
}
